package com.lm.scau.dyb.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lm.scau.dyb.R;
import com.lm.scau.dyb.activity.base.BaseActivity;
import com.lm.scau.dyb.activity.base.CustomActicity;
import com.lm.scau.dyb.activity.base.LoginActivity;
import com.lm.scau.dyb.adapter.UserItemAdapter;
import com.lm.scau.dyb.utils.FlagHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function:
 * auth：liming on 2016/8/5 15:55
 * mail：liming@wxchina.com
 */
public class UserFragment extends Fragment {

    private ListView listView;
    private LinearLayout userLayout;
    private List dataList;
    private TextView userNameView;
    private int[] iconTitleIds = new int[]{R.drawable.history,R.drawable.collect,R.drawable.record,R.drawable.item_vip,R.drawable.setting};
    private String[] itemTitles = new String[]{"播放历史","收藏列表","我的说说","成为会员","系统设置"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        if(null==dataList){
            dataList = new ArrayList<Map<String,Object>>();
            for(int i=0;i<iconTitleIds.length;i++){
                Map<String,Object> map = new HashMap<>();
                map.put(FlagHelper.ICON_IMAGE,iconTitleIds[i]);
                map.put(FlagHelper.TEXT_TITLE,itemTitles[i]);
                map.put(FlagHelper.ICON_MORE,R.drawable.dafault_more);
                dataList.add(map);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, parent, false);
        initView(view);
        initAction();
        UserItemAdapter adapter = new UserItemAdapter((BaseActivity) getActivity(),dataList);
        listView.setAdapter(adapter);
        return view;
    }

    private void initAction() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putInt("position",position);
                ((BaseActivity)getActivity()).startActivity(CustomActicity.class,bundle);
            }
        });
        userLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                if("登陆".equals(userNameView.getText())){
//                    ((BaseActivity)getActivity()).startActivity(LoginActivity.class,null);
//                }else{ //转到个人信息页面
//
//                }
                ((BaseActivity)getActivity()).startActivity(LoginActivity.class,null);
            }
        });
    }

    private void initView(View view) {
        listView = (ListView) view.findViewById(R.id.list_view_user);
        userNameView = (TextView)view.findViewById(R.id.text_view_user_name);
        userLayout = (LinearLayout)view.findViewById(R.id.layout_user_info);
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
